<template>
  <Loading :loading="loading">
    <div class="content">
      <!-- <div>
        <van-nav-bar title="加入万利">
          <template #left>
            <van-icon
              name="arrow-left"
              size="18"
              color="#999"
              @click="onClose('/joinUs')"
            /> -->
      <!-- <van-icon
              name="cross"
              size="18"
              color="#999"
              @click="onClose('/my')"
            /> -->
      <!-- </template>
        </van-nav-bar>
      </div> -->
      <NavBar></NavBar>
      <div class="banner">
        <div
          class="banner-content"
          v-for="(item, qindex) in questionList"
          :key="item.id"
          v-show="qindex == number"
        >
          <div class="label">
            {{ item.isMutiple == 'N' ? '单选题' : '多选题' }}
          </div>
          <div class="question">{{ item.question }}</div>
          <!-- 单选 -->
          <!-- <div class="answer">
          <div
            v-for="(ele, index) in item.optionList"
            :key="ele.index"
            @click="hundleSelect(index, ele, item)"
            :class="activeClass == index ? 'active' : ''"
            class="answer-item flex-row flex-align-center flex-justify-center"
          >
            <div class="optionNo">{{ ele.optionNo }}</div>
            <div class="optionContent">{{ ele.description }}</div>
          </div>
        </div>
        <div class="right flex-row flex-align-center">
          <div class="line"></div>
          <p class="text" v-show="selectShow">正确答案:{{ item.answer }}</p>
        </div>
        <div class="answer-qid">{{ index + 1 }}/9题</div>
        <van-button class="answer-next" disabled="isNext" @click="hundleNext">
          下一题
        </van-button> -->
          <!-- 多选 -->
          <div class="answer">
            <div
              v-for="(ele, index) in item.optionList"
              :key="index"
              @click="hundleSelect(index, ele, item)"
              :class="{ active: item.seleList.includes(ele.optionNo) }"
              class="answer-item flex-row flex-align-center flex-justify-center"
            >
              <div class="optionNo">{{ ele.optionNo }}</div>
              <div class="optionContent">{{ ele.description }}</div>
            </div>
          </div>
          <div class="right flex-row flex-align-center" v-show="selectShow">
            <div class="line"></div>
            <p class="text">正确答案:{{ item.answer }}</p>
          </div>
          <div class="answer-qid">
            {{ qindex + 1 }}/{{ questionList.length }}题
          </div>
          <van-button
            class="answer-next"
            :disabled="!(item.seleList.join() == item.answer)"
            @click="hundleNext()"
          >
            <span v-if="number + 1 === questionList.length">提交</span>
            <span v-else>下一题</span>
          </van-button>
        </div>
      </div>
    </div>
  </Loading>
</template>
<script>
import API from '@/services/h5/my'
import Loading from '@/components/data-loading'
import NavBar from '@/components/h5header'
export default {
  components: { Loading, NavBar },
  data() {
    return {
      activeClass: -1,
      questionList: [],
      number: 0,
      arr: [],
      selectShow: false,
      isNext: false,
      loading: false
    }
  },
  methods: {
    onClose(path) {
      this.$dialog
        .confirm({
          title: '此页面信息未保存，确认离开页面吗'
        })
        .then(() => {
          this.$router.push(path)
        })
        .catch(() => {})
    },
    getquestion() {
      this.loading = true
      API.question().then(res => {
        if (res.data && res.data.code === 1000) {
          let data = []
          res.data.data.map(qitem => {
            data.push({
              ...qitem,
              seleList: []
            })
          })
          this.questionList = data
        }
        this.loading = false
      })
    },
    hundleSelect(index, ele, qitem) {
      this.selectShow = true
      let data = qitem.seleList
      if (qitem.isMutiple === 'Y') {
        if (data.includes(ele.optionNo)) {
          data.splice(data.indexOf(ele.optionNo), 1)
        } else {
          data.push(ele.optionNo)
        }
      } else {
        if (data.includes(ele.optionNo)) {
          data.splice(data.indexOf(ele.optionNo), 1)
        } else {
          data.splice(0, 1)
          data.push(ele.optionNo)
        }
      }
    },
    async hundleNext() {
      if (this.number + 1 !== this.questionList.length) {
        this.number = this.number + 1
        this.selectShow = false
      } else {
        let obj = this.$route.query
        let area = obj.area.split('/')
        let params = {
          city: area[1],
          degree: obj.degree,
          mobile: obj.mobile,
          name: obj.username,
          province: area[0]
        }
        let res = await API.joinAddInfo(params)
        if (res.data && res.data.code === 1000) {
          this.$router.push({
            path: '/joinUs',
            query: {
              answer: true
            }
          })
        }
      }
    }
  },
  created() {
    this.getquestion()
  }
}
</script>
<style scoped lang="less">
.content {
  background: url('./img/answer.png') no-repeat;
  height: 100vh;
}
.banner {
  height: calc(100vh - 38px);
  padding: 15px;
  box-sizing: border-box;
}
.banner-content {
  background: #fff;
  box-sizing: border-box;
  height: calc(100vh - 46px - 30px);
  border-radius: 10px;
  overflow: hidden;
  position: relative;
}
.label {
  width: 66px;
  height: 19px;
  line-height: 19px;
  // background-color: #f68257;
  background-color: @c-color-primary;
  border-radius: 0 50px 50px 0;
  top: 20px;
  color: #fff;
  font-size: 16px;
  text-align: center;
  margin-top: 22px;
}
.question {
  width: 315px;
  padding: 15px;
  font-size: 18px;
  color: #333;
  font-weight: bold;
  margin-top: 40px;
}
.answer {
  margin-top: 15px;
}
.answer-item {
  width: 260px;
  margin: 0 auto;
  margin-top: 20px;
  font-size: 15px;
  color: #333;
  box-shadow: rgba(0, 0, 0, 0.06) 0 0 12.5px 0;
  border-radius: 20px;
  padding: 12px 15px;
}
.optionContent {
  margin-left: 6px;
}
.right {
  margin-left: 35px;
  margin-top: 22px;
  line-height: 16px;
}
.line {
  width: 4px;
  height: 20px;
  background-color: @c-color-primary;
  border-radius: 2px;
}
.text {
  font-size: 16px;
  font-weight: bold;
  margin-left: 18px;
}
.answer-qid {
  font-size: 15px;
  position: absolute;
  bottom: 25px;
  left: 15px;
}
.answer-next {
  width: 164px;
  height: 40px;
  border-radius: 20px;
  background-color: @c-color-primary;
  color: #fff;
  font-size: 13px;
  position: absolute;
  bottom: 10px;
  right: 15px;
  font-weight: bold;
}
.active {
  background-color: @c-color-primary;
  color: #fff;
}
</style>
